﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModel.DBService;
using DataModel.Entity;

namespace MisProduct
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private UserService Service = new UserService();

        private void LoginBtn_Click(object sender, EventArgs e)
        {
            ErrorTxt.Text = string.Empty;
            string username = usernameTxt.Text;
            string password = passwordTxt.Text;
            if (string.IsNullOrWhiteSpace(username))
            {
                ErrorTxt.Text = "请输入用户名";
                usernameTxt.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                ErrorTxt.Text = "请输入密码";
                passwordTxt.Focus();
                return;
            }
            User user = Service.Login(username, password);
            if (user == null)
            {
                ErrorTxt.Text = "用户名密码不对，请重新输入";
                return;
            }
            Application.Run(new MainForm(user));
            this.Close();

        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("您确认退出吗？", "提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                this.Close();
                Application.Exit(); 
            }
        }
    }
}
